﻿using HPSocket.Tcp;
using MCMesServer.Model;
using MCMesServer.PublicLogInfo;
using MCMesServer.ServerFunctions.StateFunc;
using MCMesServer.ServerReceiveFunctions.GenerateProcessSheet;
using MCMesServer.ServerReceiveFunctions.GenerateProducePlan;
using MCMesServer.ServerReceiveFunctions.PromoteProcessSheet;
using MCMesServer.ServerReceiveFunctions.SelectProcessSheetAndPlan;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.NetConnectFunc
{
    public class CommonConnectFunc
    {
        /// <summary>
        /// 处理接收到的信息
        /// </summary>
        /// <param name="tcpServer"></param>
        /// <param name="model"></param>
        /// <param name="connId"></param>
        public static void ReceiveMessageHandler(TcpPackServer tcpServer, ServerModel model, IntPtr connId)
        {
            try
            {
                PublicLogInfoFunc.RecordReceiveMessage(model.Step.ToString(), model.MachineNum, model.Operation.ToString(), model.Message);
                switch (model.Step)
                {
                    case Steps.StateUpload:
                        StateMsgServerHandlerFunc.HandleReceiveMsg(model, connId);
                        break;
                    case Steps.GenerateProcessSheet:
                        GenerateProcessSheetHandlerFunc.HandleReceiveMsg(model, connId);
                        break;
                    case Steps.PromoteProcessSheet:
                        PromoteProcessSheetHandlerFunc.HandleReceiveMsg(model, connId);
                        break;
                    case Steps.GenerateProducePlan:
                        GenerateProducePlanHandlerFunc.HandleReceiveMsg(model, connId);
                        break;
                    case Steps.SelectProcessSheetAndPlan:
                        SelectProcessSheetAndPlanHandlerFunc.HandleReceiveMsg(model, connId);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage($"[{model.Step}]发生接收错误，错误信息内容：{model.Message}", ex.ToString());
                return;
            }
        }

        /// <summary>
        /// 发送内部信息至客户端
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="machineNum"></param>
        /// <param name="operation"></param>
        /// <param name="step"></param>
        /// <param name="resCode"></param>
        /// <param name="result"></param>
        public static void SendInterClientModelFunc(IntPtr connId, string machineNum, Operations operation, Steps step, ServerResCode resCode, string result)
        {
            try
            {
                ClientModel clientModel = new ClientModel()
                {
                    MachineNum = machineNum,
                    Operation = operation,
                    Step = step,
                    ResCode = resCode,
                    Result = result
                };
                MainWindow.interServer.SendClientModel(connId, clientModel);
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage(operation.ToString(), $"设备号：{machineNum} DTO：{result} ##ErrorMessage:{ex.ToString()}");
                SendServerError(connId, machineNum, operation, step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
            }
        }

        public static void SendInterServerModelFunc(IntPtr connId, ServerModel model)
        {
            try
            {
                MainWindow.interServer.SendServerModel(connId, model);
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage(model.Operation.ToString(), $"设备号：{model.MachineNum} DTO：{model.Message} ##ErrorMessage:{ex.ToString()}");
                SendServerError(connId, model.MachineNum, model.Operation, model.Step, ServerResCode.ERR_UnExpectOfState, ex.ToString());
            }
        }

        /// <summary>
        /// 发送服务器端错误信息
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="machineNum"></param>
        /// <param name="operation"></param>
        /// <param name="step"></param>
        /// <param name="code"></param>
        /// <param name="errorMsg"></param>
        public static void SendServerError(IntPtr connId, string machineNum, Operations operation, Steps step, ServerResCode code, string errorMsg)
        {
            try
            {
                string result = $"###ServerError:{errorMsg.ToString()}";
                ServerModel serverModel = new ServerModel()
                {
                    MachineNum = machineNum,
                    Operation = operation,
                    Step = step,
                    ResCode = code,
                    Message = result
                };
                MainWindow.interServer.SendServerModel(connId, serverModel);
            }
            catch (Exception ex)
            {
                PublicLogInfoFunc.RecordManualErrorMessage("", $"错误信息：{ex.ToString()}");
            }
        }
    }
}
